<template>
  <div id="enterprise">
    <nav>
      <el-input placeholder="输入关键字" style="width:400px" v-model="keyword" v-if="is_Admin==1"></el-input>
<!--      <el-select v-model="is_hot" clearable placeholder="热榜" v-if="is_Admin==1">-->
<!--        <el-option-->
<!--          v-for="item in hotList"-->
<!--          :key="item.value"-->
<!--          :label="item.label"-->
<!--          :value="item.value">-->
<!--        </el-option>-->
<!--      </el-select>-->
      <el-button type="primary" @click="getCharacterList" v-if="is_Admin==1">检索</el-button>
      <el-button type="primary" @click="showDialog" v-if="is_Admin==1||tableData.length==0">添加</el-button>
      <!-- <demo v-model="foo"></demo> -->
    </nav>

    <el-table
      class="table-first"
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="date"
        label="人物头像"
        width="180">
        <template slot-scope="scope">
          <div>
            <img :src="scope.row.img_url" alt="" style="width:60px;border-radius:50%;height:60px;object-fit:cover">
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="人物名称"
        width="180">
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="type_name"-->
<!--        label="人物类型">-->
<!--      </el-table-column>-->
      <!-- <el-table-column
          prop="address"
          label="简介">
      </el-table-column> -->
      <el-table-column
        prop="address"
        label="地区">
        <template slot-scope="scope">
          <div>
            <div>{{scope.row.province}}{{scope.row.city}}{{scope.row.address_detail}}</div>
          </div>
        </template>
      </el-table-column>
<!--      <el-table-column-->
<!--        label="热榜">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tag type="success" @click="updateIsHotStatus(scope.row.id)" v-if="scope.row.is_hot=='1'" style="cursor: default">是</el-tag>-->
<!--          <el-tag type="warning" @click="updateIsHotStatus(scope.row.id)" v-if="scope.row.is_hot=='0'" style="cursor: default">否</el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column
        prop="sort"
        label="排序">
      </el-table-column>
      <el-table-column
        prop="created_at"
        label="创建时间">
      </el-table-column>
      <el-table-column prop="link_url" label="链接" width="100" sortable>
        <template slot-scope="scope">
          <div>
            <el-button type="primary"
                       v-clipboard:copy="scope.row.share_url"
                       v-clipboard:success="onCopy"
                       size="mini"
                       v-clipboard:error="onError">复制链接
            </el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="360">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editorPerson(scope.row.id)">编辑</el-button>
          <el-button type="danger" class="danger-btn" size="mini" @click="delCharacter(scope.row.id)">删除</el-button>
          <el-button type="Success" size="mini" @click="showDetailDialog(scope.row.id)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="24" class="footer-bar">
      <div class="page-code">
        <el-pagination
          background
          :page-size="16"
          layout="prev, pager, next"
          :total="totalPage"
          @current-change="pageChange"
        >
        </el-pagination>
      </div>
    </el-col>


    <!-- 添加，编辑 -->
    <el-dialog :title="dialogTit" :visible.sync="addFormVisible" @close="initAddform" width="50%"
               :close-on-click-modal="false">
      <el-form label-width="110px" label-position="left">
        <el-form-item label="人物头像">
          <el-upload
            class="avatar-uploader avatar"
            action="https://wxapp.gzmmkj.cn/api/file/upload-img"
            name="photo"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="addForm.personImg" :src="addForm.personImg" style="width: 100%;height: 100%;">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <img v-if="addForm.personImg" src="http://rzkeji.oss-cn-shenzhen.aliyuncs.com/web/mingmei/smallicon/icon/del-Image.png"  class="del-icon" @click.stop="del('photo')">
          </el-upload>
        </el-form-item>
        <el-form-item label="人物名称">
          <el-input v-model="addForm.personName" auto-complete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input v-model="addForm.desc" :rows="4" type="textarea" auto-complete="off" style="width:300px"></el-input>
        </el-form-item>
<!--        <el-form-item label="人物类型">-->
<!--          <el-select v-model="addForm.personType" placeholder="请选择">-->
<!--            <el-option-->
<!--              v-for="item in seletOptions"-->
<!--              :key="item.value"-->
<!--              :label="item.label"-->
<!--              :value="item.value">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="机构" v-if="is_Admin==1">-->
<!--          <el-select v-model="addForm.is_hot" placeholder="请选择">-->
<!--            <el-option-->
<!--              v-for="item in hotLists"-->
<!--              :key="item.value"-->
<!--              :label="item.label"-->
<!--              :value="item.value">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="地区">
          <el-cascader
            style="width:300px"
            placeholder="选择地址"
            :options="PcCode"
            :props="{value:'label',label:'label'}"
            v-model="addForm.provinces"
            class="cascader"
            filterable></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址(选填)">
          <el-input v-model="addForm.address" type="text" auto-complete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="点赞数(选填)" v-if="is_Admin==1">
          <el-input v-model="addForm.like_num" placeholder="请输入点赞数" auto-complete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="联系电话(选填)">
          <el-input v-model="addForm.contact_number" type="text" auto-complete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="服务电话(选填)">
          <el-input v-model="addForm.contact_phone" type="text" auto-complete="off" style="width:300px"></el-input>
        </el-form-item>
<!--        <el-form-item label="公众号(选填)">-->
<!--          <el-upload-->
<!--            class="avatar-uploader"-->
<!--            action="https://wxapp.gzmmkj.cn/api/file/upload-img"-->
<!--            name="photo"-->
<!--            :show-file-list="false"-->
<!--            :on-success="handleWechatSuccess"-->
<!--          >-->
<!--            <img v-if="addForm.wechat_img_url" :src="addForm.wechat_img_url" class="avatar">-->
<!--            <i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
<!--          </el-upload>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="小程序(选填)">-->
<!--          <el-upload-->
<!--            class="avatar-uploader avatar"-->
<!--            action="https://wxapp.gzmmkj.cn/api/file/upload-img"-->
<!--            name="photo"-->
<!--            list-type="picture-card"-->
<!--            :show-file-list="false"-->
<!--            :on-success="handleWxappSuccess"-->
<!--          >-->
<!--            <img v-if="addForm.wxapp_img_url" :src="addForm.wxapp_img_url" style="width: 100%;height: 100%">-->
<!--            <i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
<!--            <img v-if="addForm.wxapp_img_url" src="http://rzkeji.oss-cn-shenzhen.aliyuncs.com/web/mingmei/smallicon/icon/del-Image.png"  class="del-icon" @click.stop="del('wxapp')">-->
<!--          </el-upload>-->
<!--        </el-form-item>-->
        <el-form-item label="网站(选填)">
          <el-input v-model="addForm.website" type="text" auto-complete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="二维码底部文字(选填)">
          <el-input v-model="addForm.codeText" type="textarea" maxlength="11" placeholder="字数不能超过11个"
                    auto-complete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="二维码Logo(选填)">
          <el-upload
            class="avatar-uploader avatar"
            action="https://wxapp.gzmmkj.cn/api/file/upload-img"
            name="photo"
            :show-file-list="false"
            :on-success="handleQrLogoSuccess"
          >
            <img v-if="addForm.qr_customize_logo_url" :src="addForm.qr_customize_logo_url" style="width: 100%;height: 100%">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <img v-if="addForm.qr_customize_logo_url" src="http://rzkeji.oss-cn-shenzhen.aliyuncs.com/web/mingmei/smallicon/icon/del-Image.png"  class="del-icon" @click.stop="del('qr')">
          </el-upload>
          <P class="text">建议上传圆形正比例图片</P>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="addForm.sortNum" auto-complete="off" style="width:300px" type="number"></el-input>
        </el-form-item>
        <el-form-item label="附加功能">
          <el-switch
            style="display: block"
            v-model="addForm.attach_function"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="是"
            inactive-text="否">
          </el-switch>
        </el-form-item>
        <el-form-item label="置顶视频/图片" prop="Video">

          <audioPicUpload v-model="topContent" v-if="topContentShow"></audioPicUpload>
        </el-form-item>

        <editor v-model="addContent" v-if="editorShow"></editor>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="setCharacter">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="详情" :visible.sync="detailFormVisible" @close="initDetailform" width="50%" class="detailDialog">
      <el-form>
        <el-form-item>
          <div v-if="topContent.type=='img'">
            <img :src="topContent.src" alt="">
          </div>
          <div v-if="topContent.type=='video'">
            <video :src="topContent.src" controls style="width:100%;height:300px"
                   :poster="topContent.video_post_src"></video>
          </div>
        </el-form-item>
        <el-form-item>
          <div v-html="addContent"></div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import PcCode from '../../assets/json/origin.json';
  import audioPicUpload from '@/components/audioPicUpload';
  import editor from '@/components/editor';

  export default {
    name: 'person',
    components: {
      editor,
      audioPicUpload,
    },
    data() {
      return {
        is_Admin: window.localStorage.getItem('is_admin'),
        keyword: '',
        hotList: [
          {label: '全部',value: 0},
          {label: '是',value: 2},
          {label: '否',value: 1}
        ],
        hotLists: [
          {label: '是',value: '1'},
          {label: '否',value: '0'}
        ],
        is_hot: '',
        page: 1,
        totalPage: 0,
        PcCode,
        dialogTit: '',
        addFormVisible: false,
        addContent: '',//富文本
        editorShow: true,//显示富文本

        topType: '',//区分置顶的是图片还是视频
        uploadUrl: '',//
        addForm: {
          personImgId: '',
          personImg: '',
          personName: '',
          personType: '',//人物类型
          desc: '',//简介
          sortNum: '',//排序
          codeText: '',//小程序描述
          provinces: '', //地区
          address: '',//详细地址
          is_hot: '0',
          contact_number: '',//联系电话
          contact_phone: '',//服务电话
          website: '',//企业网站
          wechat_img_id: '',
          wechat_img_url: '',
          wxapp_img_id: '',
          wxapp_img_url: '',
          qr_customize_logo_id: '',
          qr_customize_logo_url: '',
          like_num: '',
          attach_function: true
        },
        topContent: {//置顶的信息

        },
        seletOptions: [
          {
            value: '1',
            label: '人物榜'
          },
          {
            value: '0',
            label: '人物云集'
          }
        ],
        topContentShow: true,
        tableData: [],
        detailFormVisible: false//详情页的显示
      }
    },
    methods: {
      del(name){
        if (name == 'photo') {
          this.addForm.personImgId = '';
          this.addForm.personImg = '';
        }else if (name == 'wxapp'){
          this.addForm.wxapp_img_id = '';
          this.addForm.wxapp_img_url = '';
        }else if (name = 'qr'){
          this.addForm.qr_customize_logo_id = '';
          this.addForm.qr_customize_logo_url = '';
        }
      },
      async updateIsHotStatus(id){
        let params = {
          type: 'character',
          character_id: id
        }
        const res = await this.$api.updateIsHotStatus(params);
        if (res.data.level == 'success'){
          this.$message({
            type: 'success',
            message: res.data.message
          })
          this.getCharacterList();
        } else {
          this.$message({
            type: 'warning',
            message: res.data.message
          })
        }
      },
      onCopy(e) {
        this.$message({
          type: 'success',
          message: '复制成功'
        })
      },
      onError(erro) {
        this.$message({
          type: 'error',
          message: '复制失败'
        })
      },
      showDialog() {
        //新增
        this.addFormVisible = true;
      },
      addPerson() {
        //提交表单
        this.addFormVisible = false;
        this.initAddform();
      },
      editorPerson(id) {
        //编辑
        this.id = id;
        this.addFormVisible = true;
        this.getCharacter()
      },
      delCharacter(id) {
        //删除
        const judge = this.$confirm('删除该人物?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await this.$api.delCharacter({id});
          if (res.data.level == 'success') {
            this.getCharacterList();
            this.$message({
              type: 'success',
              message: res.data.message
            });
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }

        }).catch(() => {

        });
      },
      showDetailDialog(id) {
        //查看详情
        this.id = id;
        this.detailFormVisible = true;
        this.getCharacter()
      },
      handleAvatarSuccess(res, file) {
        //上传照片成功
        if (res.data) {
          this.addForm.personImg = res.data.img_url;
          this.addForm.personImgId = res.data.id;
          this.$forceUpdate()
        }
      },
      handleWechatSuccess(res, file){
        //上传照片成功
        if (res.data) {
          this.addForm.wechat_img_url = res.data.img_url;
          this.addForm.wechat_img_id = res.data.id;
          this.$forceUpdate()
        }
      },
      handleWxappSuccess(res, file){
        //上传照片成功
        if (res.data) {
          this.addForm.wxapp_img_url = res.data.img_url;
          this.addForm.wxapp_img_id = res.data.id;
          this.$forceUpdate()
        }
      },
      handleQrLogoSuccess(res, file){
        //上传照片成功
        if (res.data) {
          this.addForm.qr_customize_logo_url = res.data.img_url;
          this.addForm.qr_customize_logo_id = res.data.id;
          this.$forceUpdate()
        }
      },
      initAddform() {
        //初始化数据
        if (this.is_Admin==0){
          return;
        }
        this.id = ''
        this.addForm = {
          personImgId: '',
          personImg: '',
          personName: '',
          personType: '',//人物类型
          desc: '',//简介
          sortNum: '',//排序
          codeText: '',//小程序描述
          provinces: '', //地区
          address: '',//详细地址
          is_hot: '',
          contact_number: '',//联系电话
          contact_phone: '',//服务电话
          website: '',//企业网站
          wechat_img_id: '',
          wechat_img_url: '',
          wxapp_img_id: '',
          wxapp_img_url: '',
          qr_customize_logo_id: '',
          qr_customize_logo_url: '',
          like_num: '',
          attach_function: true,
        };
        this.topContent = {};
        this.editorShow = false;
        this.topContentShow = false;
        this.addContent = '';
        setTimeout(() => {
          this.editorShow = true;
          this.topContentShow = true;
        }, 500)
      },
      initDetailform() {
        //初始化查看详情
        this.initAddform()
      },
      closeDialog() {
        this.addFormVisible = false;
        this.initAddform();
      },
      async getCharacterList() {
        let params = {
          page: this.page,
          limit: 16,
          keyword: this.keyword,
          is_hot : this.is_hot==0?'':(this.is_hot-1),
          user_token: window.localStorage.getItem('user_token')
        }
        let res = await this.$api.getCharacterList(params);
        if (res.data.level == 'success') {
          this.tableData = res.data.data;
          this.totalPage = res.data.page_info.total
        }
      },
      pageChange(e) {
        //翻页
        this.page = e;
        this.getCharacterList();
      },
      async setCharacter() {
        //新增修改
        var top_id = '', video_id = '', video_img_id;
        if (this.topContent.type == 'video') {
          video_id = this.topContent.srcId;
          video_img_id = this.topContent.video_post_id;
        } else {
          top_id = this.topContent.srcId;

        }
        let data = this.addForm;
        let province = data.provinces ? data.provinces[0] : '';
        let city = data.provinces ? data.provinces[1] : '';
        let area = data.provinces ? data.provinces[2] : '';
        let params = {
          img_id: data.personImgId,
          name: data.personName,
          industry_id: data.industry_id,
          province: province,
          city: city,
          area: area,
          type: data.personType,
          address_detail:this.addForm.address,
          sort: data.sortNum,
          video_id: video_id,
          top_id: top_id,
          video_img_id: video_img_id,
          desc: data.desc,
          content: this.addContent,
          id: this.id,
          qr_title: data.codeText,
          is_hot: data.is_hot,
          contact_number: data.contact_number,
          contact_phone: data.contact_phone,
          website: data.website,
          wechat_img_id: data.wechat_img_id,
          wxapp_img_id: data.wxapp_img_id,
          like_num: data.like_num,
          attach_function: data.attach_function?'1':'0',
          qr_customize_logo_id: data.qr_customize_logo_id,
          photo: data.qr_customize_logo_url
        }
        let res = await this.$api.setCharacter(params);
        if (res.data.level == 'success') {
          this.$message({
            message: res.data.message,
            type: 'success'
          });
          this.is_hot = '';
          this.getCharacterList()
          this.addFormVisible = false;
        } else {
          this.$message({
            message: res.data.message,
            type: 'error'
          });
        }
      },
      async getCharacter() {
        let res = await this.$api.getCharacter({id: this.id});
        if (res.data.level == 'success') {
          let data = res.data.data;
          console.log(data)
          this.addForm.personImgId = data.thumb_id;
          this.addForm.personImg = data.img_url;
          this.addForm.personName = data.name;
          this.addForm.sortNum = data['sort'];
          this.addForm.codeText = data.qr_title;
          this.addForm.provinces=[data.province,data.city,data.area];
          this.addForm.address=data.address_detail;
          this.addForm.is_hot = data.is_hot;
          this.addForm.desc = data.desc;
          this.addForm.personType = data.type;
          this.addForm.contact_number = data.contact_number;
          this.addForm.contact_phone = data.contact_phone;
          this.addForm.website = data.website;
          this.addForm.wechat_img_id = data.wechat_img_id;
          this.addForm.wechat_img_url = data.wechat_img_url;
          this.addForm.wxapp_img_id = data.wxapp_img_id;
          this.addForm.wxapp_img_url = data.wxapp_img_url;
          this.addForm.like_num = data.like_num;
          this.addForm.qr_customize_logo_id = data.qr_customize_logo_id;
          this.addForm.qr_customize_logo_url = data.qr_customize_logo_url;
          this.addForm.attach_function = data.attach_function==1?true:false;
          if (data.video_url) {
            this.topContent = {
              src: data.video_url,
              srcId: data.video_id,
              type: 'video',
              video_post_src: data.video_img_url,
              video_post_id: data.video_img_id,
            }
          } else if
          (data.top_url) {
            this.topContent = {
              src: data.top_url,
              srcId: data.top_id,
              type: 'img',

            }
          }
          this.addContent = data.content;
          this.editorShow = false;
          setTimeout(() => {
            this.editorShow = true;
          }, 500)
        }

      },

    },
    mounted() {
      // setTimeout(()=>{
      //      this.topContent={
      //         src:'http://file.rzkeji.com/uploads/2019/10/21/b4d41a81f477dd481bae9f21cdd31d20.mp4',
      //         srcId:1,
      //         type:'video',
      //         video_post_src:'http://file.rzkeji.com//web/shanghui/uploads/2019/8/20/f02a683c3f6f340851dda5added72d90.jpg',
      //         video_post_id:'2'
      //     }
      // },2000)
      this.getCharacterList();
    },
  }
</script>
<style lang="scss" scoped>
  .dialog-footer {
    text-align: center;
  }

  nav {
    background: #eee;
    padding: 20px;
    // position: fixed;
    width: 100%;
    z-index: 99;
  }

  // .table-first{
  //     padding-top:100px;
  // }

  //上传图片样式
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 1px dashed #d9d9d9;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    object-fit: contain;
    position: relative;
  }
  .del-icon{
    position: absolute;
    top: -10px;
    right: -10px;
    height: 20px;
    width: 20px;
    z-index: 3000;
    background: #ffffff;
    border-radius: 360px;
  }
</style>
<style>
  .detailDialog img {
    max-width: 300px;
  }
  .el-upload--picture-card{
    width: 100%;
    height: 100%;
    line-height: auto;
  }
  .el-upload{
    width: 100%;
    height: 100%;
  }
</style>
